package com.shanzmoo.admin.web.product;


import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.product.entity.ProductUnitEntity;
import com.shanzmoo.db.product.entity.vo.ProductUnitVO;
import com.shanzmoo.db.product.entity.vo.UnitQueryVO;
import com.shanzmoo.db.product.service.IProductUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-06
 */
@Api(tags = "商品-商品单位")
@RestController
@RequestMapping("/api/product/unit")
public class ProductUnitController {

    @Autowired
    private IProductUnitService productUnitService;

    @ApiOperation(value = "商品单位list")
    @GetMapping("/unitList")
    public SHttpResponse<PageRespBase<ProductUnitEntity>> unitList(UnitQueryVO unitQueryVO) {
        SResponse<PageRespBase<ProductUnitEntity>> re = productUnitService.getUnitList(unitQueryVO);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "添加商品单位")
    @GetMapping("/addUnit")
    public SHttpResponse<Boolean> addUnit(ProductUnitEntity productUnitEntity) {
        productUnitEntity.setCreateUser(CurrentUserCache.getUserId());
        productUnitService.save(productUnitEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "删除商品单位")
    @GetMapping("/delUnitById")
    public SHttpResponse<Boolean> delBrandById(@RequestParam(value = "unitId") Integer unitId) {

        ProductUnitEntity productUnitEntity = new ProductUnitEntity();
        productUnitEntity.setId(unitId);
        productUnitEntity.setUpdateUser(CurrentUserCache.getUserId());
        productUnitEntity.setDeleted(true);
        productUnitService.updateById(productUnitEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "商品单位选择列表")
    @GetMapping("/selectList")
    public SHttpResponse<List<ProductUnitVO>> selectList() {
        SResponse<List<ProductUnitVO>> re = productUnitService.getSelectList();
        return SHttpResponse.transform(re);
    }

}
